home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
challenge
/
12.09-Sep96
/
Testcode96.09R1.sit
/
Testcode96.09R1
/
1 Bee Swarm R1
/
swarm.java
< prev
next >
Wrap
Text File
|
1996-08-19
|
11KB
|
367 lines
/**
* MODIFIED FROM THE ORIGINAL BY Bob Boonstra for the Programmer's Challenge
*
* Title: BeeSwarm
* Type: Applet
* Source: swarm.java
* Description: Displays a window with swarming bees chasing a lone queen bee.
* The queen bee darts about the screen and the drones follow in her
* wake, swarming and buzzing about, changing colors as they go.
*
* Author: Michael Nosal
* Symantec Corporation ©1996
*/
/*****************************************************
* import java.awt.*;
* import java.lang.*;
******************************************************/
/** ---------------------------------------------------------------------------------
* Class: swarm - manages the swarm of bees and the queen.
* We start the queen and the drones at the same location. random jitter to the position of
* each bee moves them around the screen. The queen won't go off the edge of the window,
* while the bees will. The bees also change color.
*/
public class swarm extends java.applet.Applet /*implements Runnable*/ {
int numBees,numIters;;
int queenBeex,queenBeey,queenBeedX,queenBeedY,queenBeeoldX,queenBeeoldY;
int dronesX[],dronesY[],dronesDX[],dronesDY[],dronesOldX[],dronesOldY[];
/*****************************************************
* Color queenBeebeeColor,dronesbeeColor[];
* String sumString;
******************************************************/
int hue;
int MAXX;
int MAXY;
int xMin, xMax, yMin, yMax;
int MAXBV; // max bee velocity (pixels per frame)
int MAXQV; // max queen velocity (pixels per frame)
int MAXQA; // max queen acceleration
int MAXBA; // max bee acceleration
int borderWidth; // queen won't go nearer than this to the edge of the window
int myRandSeed;
int myRandMultiplier;
int sum;
/*****************************************************
* // for double buffering to prevent flicker
* Image offScreenImage;
* Graphics offScreen;
******************************************************/
int qstartX, qstartY, qstartV;
int b;
/** ---------------------------------------------------------------------------------
* init() - creates the queen bee and the drones, and trys to set up the offscreen graphics
* world for double-buffering the animation.
*/
void myInit(){
hue = 270;
MAXX = 400;
MAXY = 400;
xMin = 10000;
xMax = -10000;
yMin = 10000;
yMax = -10000;
MAXBV = 9; // max bee velocity (pixels per frame)
MAXQV = 12; // max queen velocity (pixels per frame)
MAXQA = 5; // max queen acceleration
MAXBA = 3; // max bee acceleration
borderWidth = 20; // queen won't go nearer than this to the edge of the window
myRandSeed = 0x01234567;
myRandMultiplier = 0x77777777;
dronesX = new int[numBees];
dronesY = new int[numBees];
dronesDX = new int[numBees];
dronesDY = new int[numBees];
dronesOldX = new int[numBees];
dronesOldY = new int[numBees];
/*****************************************************
* dronesbeeColor = new Color[numBees];
******************************************************/
/*****************************************************
* resize(MAXX,MAXY);
******************************************************/
//create the queen bee
//we want the starting X pos to be within some border of the edge of the window
qstartX = RangedRdm(borderWidth,MAXX - borderWidth);
qstartY = RangedRdm(borderWidth,MAXY - borderWidth);
queenBeex = qstartX;
queenBeey = qstartY;
queenBeedX = 0;
queenBeedY = 0;
queenBeeoldX = queenBeex;
queenBeeoldY = queenBeey;
/*****************************************************
* queenBeebeeColor = Color.red;
******************************************************/
// create the new bees
for (int i=0; i < numBees; i++) {
dronesX[i] = qstartX;
dronesY[i] = qstartY;
dronesDX[i] = RandZip(MAXBA);
dronesDY[i] = RandZip(MAXBA);
dronesOldX[i] = dronesX[i];
dronesOldY[i] = dronesY[i];
}
/*****************************************************
* offScreenImage = createImage (MAXX, MAXY);
* offScreen = offScreenImage.getGraphics ();
* offScreen.setColor(Color.black);
* offScreen.fillRect(0,0,MAXX,MAXY);
******************************************************/
} // end of swarm.init
int myRun()
{
numBees = 50;
numIters = 5000;
myInit();
for (int loopCount=0; loopCount<numIters; loopCount++) {
updateBees();
// repaint();
}
sum = 0;
for (int b=0; b<numBees; b++) {
sum += dronesX[b];
sum += dronesY[b];
}
return sum;
}
/** --------------------------------------------------------------------------
* start() - initializes a new thread
*
*/
public void start() {
sum = myRun();
} //end of swarm.start
/** --------------------------------------------------------------------------
* destroy() - we dispose of our offscreen graphics world to free up resources
*
*/
/*****************************************************
* public void destroy() {
* if (offScreen != null) {
* offScreen.dispose();
* }
* } // end of destroy
******************************************************/
/** --------------------------------------------------------------------------
* paint() - this decides if we can paint on the offscreen graphics world or if we go
* straight to the screen.
*/
/*****************************************************
* public void paint(Graphics g)
* {
* if (offScreen != null)
* { // double-buffering available
* offScreen.setColor(Color.black);
* offScreen.fillRect(0,0,MAXX,MAXY);
* paintApplet(offScreen);
* g.drawImage (offScreenImage, 0, 0, this);
* //g.clipRect(0,0,MAXX,MAXY);
* }
* else
* { // no double-buffering
* g.setColor(Color.black);
* g.fillRect(0,0,MAXX,MAXY);
* paintApplet (g);
* }
* } // end of swarm.paint;
*
******************************************************/
/** --------------------------------------------------------------------------
* paintApplet() - This makes the actual calls to draw the bees on the screen.
*
*/
/** --------------------------------------------------------------------------
* public void paintApplet(Graphics g)
* {
* g.setColor(Color.black);
*
* g.fillRect(xMin-MAXQV,yMin-MAXQV,(xMax+MAXQV+MAXQV)-xMin,(yMax+MAXQV+MAXQV)-yMin);
* g.setColor(queenBeebeeColor);
*
* g.drawLine(queenBeeoldX, queenBeeoldY, queenBeex, queenBeey);
*
* g.setColor(dronesbeeColor[0]);
* for (b=0; b < numBees; b++)
* {
* g.drawLine(dronesOldX[b],dronesOldY[b],dronesX[b],dronesY[b]);
* }
* // just for fun, draw a white dot at the bee's head
* g.setColor(Color.white);
* for (b=0; b < numBees; b++)
* {
* g.drawLine(dronesX[b],dronesY[b],dronesX[b] ,dronesY[b]);
* }
* sumString = Integer.toString(sum);
* g.drawString(sumString,20,20);
*
* } // end paintApplet
******************************************************/
int myMin(int a,int b)
{
if (a<b) return a;
return b;
}
int myMax(int a,int b)
{
if (a>b) return a;
return b;
}
/** --------------------------------------------------------------------------
* update() - this handles the computation of the bee's position. We also keep track
* of the bounding box around the swarm, so we only have to update as small a
* region of the screen as possible.
*/
public void updateBees()
{ // variables for computing bounding rect for swarm
xMin = 10000; xMax = -10000; yMin = 10000; yMax = -10000;
int dtx, dty, dist; // distances from bee to queen
xMin = myMin(queenBeey,xMin);
xMax = myMax(queenBeey,xMax);
yMin = myMin(queenBeey,yMin);
yMax = myMax(queenBeey,yMax);
queenBeeoldX = queenBeex;
queenBeeoldY = queenBeey;
queenBeedX += RandZip(MAXQA);
queenBeedY += RandZip(MAXQA);
// keep speed limited to maximums
if (queenBeedX > MAXQV)
queenBeedX = MAXQV;
else
if (queenBeedX < -MAXQV)
queenBeedX = -MAXQV;
if (queenBeedY > MAXQV)
queenBeedY = MAXQV;
else
if (queenBeedY < -MAXQV)
queenBeedY = -MAXQV;
// update the queen's position
queenBeex += queenBeedX;
queenBeey += queenBeedY;
// check to see if the queen hit the edge
if (queenBeex < borderWidth || queenBeex > (MAXX - borderWidth))
{
queenBeedX = -queenBeedX;
queenBeex += (queenBeedX );
}
if (queenBeey < borderWidth || queenBeey > (MAXY - borderWidth))
{
queenBeedY = -queenBeedY;
queenBeey += (queenBeedY );
}
xMin = myMin(queenBeex,xMin);
xMax = myMax(queenBeex,xMax);
xMin = myMin(queenBeeoldX,xMin);
xMax = myMax(queenBeeoldX,xMax);
yMin = myMin(queenBeey,yMin);
yMax = myMax(queenBeey,yMax);
yMin = myMin(queenBeeoldY,yMin);
yMax = myMax(queenBeeoldY,yMax);
// we do color cycling for the bees here
hue++;
// for now, all drones have the same color, so we're only setting the color of the first one.
/*****************************************************
* dronesbeeColor[0] = Color.getHSBColor((float)(hue/ 360.0),(float)1.0,(float)1.0);
******************************************************/
hue = hue % 359; // keep going around the color wheel.
for (b=0; b < numBees; b++)
{ //accelerate
dtx = queenBeex - dronesX[b];
dty = queenBeey - dronesY[b];
dist = Math.abs(dtx) + Math.abs(dty);
if (dist == 0)
dist = 1; // avoid dividing by zero
dronesOldX[b] = dronesX[b];
dronesOldY[b] = dronesY[b];
// the randZip adds some extra jitter to prevent the bees from flying in formation
dronesDX[b] += (int)((dtx * MAXBA )/dist) + RandZip(2);
dronesDY[b] += (int)((dty * MAXBA )/dist) + RandZip(2);
if (dronesDX[b] > MAXBV)
dronesDX[b] = MAXBV;
else
if (dronesDX[b] < -MAXBV)
dronesDX[b] = -MAXBV;
if (dronesDY[b] > MAXBV)
dronesDY[b] = MAXBV;
else
if (dronesDY[b] < -MAXBV)
dronesDY[b] = -MAXBV;
dronesX[b] += dronesDX[b];
dronesY[b] += dronesDY[b];
xMin = myMin(dronesX[b],xMin);
xMax = myMax(dronesX[b],xMax);
xMin = myMin(dronesOldX[b],xMin);
xMax = myMax(dronesOldX[b],xMax);
yMin = myMin(dronesY[b],yMin);
yMax = myMax(dronesY[b],yMax);
yMin = myMin(dronesOldY[b],yMin);
yMax = myMax(dronesOldY[b],yMax);
} // end for b=0 to numBees
}
/*****************************************************
* public void update (Graphics g)
* {
* updateBees();
* paint(g);
* }
******************************************************/
/** --------------------------------------------------------------------------
* RangedRdm - return a random number in the range min <-> max
*
*/
public int RangedRdm(int min, int max)
{
int range, t;
range = max - min +1;
t = myRandSeed % range;
if (t+min > max)
t = max - min;
myRandSeed *= myRandMultiplier;
myRandSeed &= 0x0000FFFF;
return (t+min);
}
/** --------------------------------------------------------------------------
* RandZip - return a value from -val to +val
*
*/
public int RandZip(int val)
{
return (RangedRdm(-(val)/2, (val)/2));
}
}